package com.example.walkera.firstexoplayerdemo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.AsyncTask;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import java.util.List;

public class MainActivity extends AppCompatActivity /*implements View.OnClickListener*/{

    private final String TAG="MainActivity";
//    private Button startButton;
    private List<VideoFileInfo> mlistVideoFileInfo;//存储搜索出来的视频文件list
    private ImageThumbnailLoader mimageThumbnailLoader;//图片加载类对象
    private VideoFileListViewAdapter mvideoFileListViewAdapter;//视频文件适配器类
    private ListView mVideoFileListView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mimageThumbnailLoader=new ImageThumbnailLoader();
        mlistVideoFileInfo=mimageThumbnailLoader.getList(this);//获取视频文件列表

        mvideoFileListViewAdapter=new VideoFileListViewAdapter(this,mlistVideoFileInfo);

        mVideoFileListView=(ListView)findViewById(R.id.video_file_list);
        //给这个listview设置一个适配器
        mVideoFileListView.setAdapter(mvideoFileListViewAdapter);
        mVideoFileListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent();
                intent.setClass(MainActivity.this,AVPlayerActivity.class);
                //填充需要传递的数据到intent
                String strVideoPath=mlistVideoFileInfo.get(i).getPath();
                intent.setData(Uri.parse(strVideoPath));
                startActivity(intent);
            }
        });

        //启动异步
        new LoadVideoImageTask().execute();

//        startButton=(Button)findViewById(R.id.start_play);
//        startButton.setOnClickListener(this);

    }



    //添加一个加载图片的函数
    private void AddBitImage(BitImage... value){
        for (BitImage image:value){
            mvideoFileListViewAdapter.addPhoto(image);
            mvideoFileListViewAdapter.notifyDataSetChanged();
        }
    }


    //添加一个私有类，用于异步加载image
    private class LoadVideoImageTask extends AsyncTask<Object,BitImage,Object>{
        @Override
        protected Object doInBackground(Object... params) {
            Bitmap bitmap = null;
            for (int i = 0; i < mlistVideoFileInfo.size(); i++) {
                bitmap = mimageThumbnailLoader.getVideoThumbnail(mlistVideoFileInfo.get(i).getPath(), 120, 120, MediaStore.Images.Thumbnails.MINI_KIND);
                if (bitmap != null) {
                    publishProgress(new BitImage(bitmap));
                }
            }
            return null;
        }

        @Override
        public void onProgressUpdate(BitImage... value) {
            AddBitImage(value);
        }
    }

//    @Override
//    public void onClick(View view){
//        switch (view.getId()){
//            case R.id.start_play:
//            {
//                Intent intent=new Intent(this,AVPlayerActivity.class);
//                intent.setData(Uri.parse("file:///mnt/internal_sd/Movies/1080p265.mp4"));
//                //启动新的activity
//                startActivity(intent);
//            }
//                break;
//            default:
//                break;
//        }
//        return;
//    }


}
